package org.example.test_qlexpress;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import org.example.test_qlexpress.function.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Test {

    public static void main(String[] args) throws Exception {
        helloworld();
        customDefineFunction();
    }

    private static void helloworld() throws Exception {
        // 创建引擎：
        ExpressRunner runner = new ExpressRunner();

        // 设置变量：
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        context.put("$1", "Hello");
        context.put("$2", "World");

        // 编写脚本：
        String script = "$1 = \"ABC\"; return $1 + $2 + '!';";

        // 执行脚本：
        String result = (String) runner.execute(script, context, null, false, false);

        // 打印返回值：
        System.out.println(result);
    }

    private static void customDefineFunction() throws Exception {
        // 创建引擎：
        ExpressRunner runner = new ExpressRunner();

        // 注册自定义函数：
        runner.addFunction("end", new EndFunction());
        runner.addFunction("sum", new SumFunction());
        runner.addFunction("avg", new AvgFunction());
        runner.addFunction("abs", new AbsFunction());
        runner.addFunction("ceil", new CeilFunction());
        runner.addFunction("floor", new FloorFunction());
        runner.addFunction("pow", new PowFunction());
        runner.addFunction("decode", new DecodeFunction());
        runner.addFunction("lt", new LtFunction());
        runner.addFunction("gt", new GtFunction());
        runner.addFunction("between", new BetweenFunction());

        // 设置变量：
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        context.put("$1", "Hello");
        context.put("$2", "World");
        context.put("$3", 5);
        context.put("$4", 2);
        context.put("$5", 50);
        context.put("$6", 123);
        context.put("$7", 321);

        // 编写脚本：
        String script =
                        getFunctionsScript() +
                        "List list = new ArrayList();\n"+
                        "list.add(\"AAAAAAAAAAAAAAAAAAAAAAAAa\");\n"+
                        "list.add(\"ZZZZZZZZZZZZZZZZZZZZZZZZz\");\n"+
                        "return concat(list);\n";

        // 执行脚本：
        String result = (String) runner.execute(script, context, null, false, false);

        // 打印返回值：
        System.out.println(result);
    }

    private static String getFunctionsScript() {
        InputStream in = Test.class.getResourceAsStream("/qlexpress/functions.txt");
        BufferedReader r = null;
        try {
            r = new BufferedReader(new InputStreamReader(in, "UTF-8"), 4096);
            StringBuilder buf = new StringBuilder();
            int c;
            while ((c = r.read()) > -1) {
                buf.append((char) c);
            }
            return buf.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (r != null) {
                try {
                    r.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
